在1.9.24
之前的版本,如果Client
要通过域名连接服务器,需要手工调用swoole_async_dns_lookup
函数,否则底层会发生阻塞。在最新的1.9.24
中底层支持了自动异步解析域名,不再需要显式调用swoole_async_dns_lookup
。
Swoole\Client
Swoole\Http\Client
Swoole\Coroutine\Client
Swoole\Coroutine\Http\Client
swoole_async_dns_lookup("www.baidu.com", function ($domain, $ip) {
$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
$client->on("connect", function(swoole_client $cli) {
$cli->send("GET / HTTP/1.1\r\n\r\n");
});
$client->on("receive", function(swoole_client $cli, $data){
echo "Receive: $data";
$cli->send(str_repeat('A', 100)."\n");
sleep(1);
});
$client->on("error", function(swoole_client $cli){
echo "error\n";
});
$client->on("close", function(swoole_client $cli){
echo "Connection close\n";
});
$client->connect( $ip, 9501);
});
$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
$client->on("connect", function(swoole_client $cli) {
$cli->send("GET / HTTP/1.1\r\n\r\n");
});
$client->on("receive", function(swoole_client $cli, $data){
echo "Receive: $data";
$cli->send(str_repeat('A', 100)."\n");
sleep(1);
});
$client->on("error", function(swoole_client $cli){
echo "error\n";
});
$client->on("close", function(swoole_client $cli){
echo "Connection close\n";
});
//底层会自动进行异步域名解析
$client->connect('www.baidu.com', 9501);